package com.example.yskjproject.ui.activity;

import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yskjproject.R;
import com.example.yskjproject.base.BaseActivity;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.databinding.ActivityClassRoomBinding;
import com.example.yskjproject.databinding.ActivitySexAlertBinding;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.utils.EventBusUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class SexAlertActivity extends BaseActivity<ActivitySexAlertBinding> {
    private ActivitySexAlertBinding mSexAlertBinding;
    private int sexType = 3;
    private Gson gson = new Gson();
    @Override
    protected ActivitySexAlertBinding getViewBinding() {
        mSexAlertBinding = ActivitySexAlertBinding.inflate(LayoutInflater.from(this));

        return mSexAlertBinding;
    }

    @Override
    protected void initData() {
        setTitle("编辑性别");
        mSexAlertBinding.rgSex.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_sex_boy) {
                sexType = 1;
            } else if (checkedId == R.id.rb_sex_girl) {
                sexType = 2;
            }
        });
        mSexAlertBinding.rbSexBoy.setChecked(true);
        if (mSexAlertBinding.rbSexBoy.isChecked()) {
            sexType = 1;
        } else {
            sexType = 2;
        }

        mSexAlertBinding.btnTrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveSexInfo();
            }
        });
    }

    private void saveSexInfo() {
        Map<String, Object> jsonParams = new HashMap<>();
        jsonParams.put("sex",sexType );
        String paramsDate = gson.toJson(jsonParams);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsDate);
        OkGo.<LzyResponse<Void>>post(Urls.UPDATE_USER).upRequestBody(requestBody).execute(new JsonCallback<LzyResponse<Void>>() {
            @Override
            public void onSuccess(Response<LzyResponse<Void>> response) {
                ToastUtil.toastLongMessage("性别修改成功");
                EventMessage eventMessage = new EventMessage(EventContent.update_sex, sexType + "");
                EventBusUtil.sendEvent(eventMessage);
                finish();
            }

            @Override
            public void onError(Response<LzyResponse<Void>> response) {
                super.onError(response);
            }
        });
    }
}
